package com.highgain.displacementtrend.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.highgain.displacementtrend.entity.*;
import com.highgain.displacementtrend.service.AlarmLogService;
import com.highgain.displacementtrend.service.AlarmParaService;
import com.highgain.displacementtrend.service.DisplacementService;
import com.highgain.displacementtrend.util.DateUtil;
import com.highgain.displacementtrend.util.Flexigrid;
import com.highgain.displacementtrend.util.PageView;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author zhanglifeng
 * @date 2021/04/13 16:16
 **/
@Controller
public class DisplacementController {

    @Resource
    private DisplacementService displacementService;


    @Resource
    private AlarmLogService alarmLogService;

    @Resource
    private AlarmParaService alarmParaService;

    @GetMapping({"/", "/index"})
    public String index(){
        return "index";
    }

    @GetMapping("/data/chart/page")
    public String dataChartPage(Model model){
        model.addAttribute("endTime", DateUtil.getCurrentDayTime());
        model.addAttribute("startTime", DateUtil.getPreNDay(-7));
        return "data/chart";
    }

    @GetMapping("/alarmLog/page")
    public String alarmLogPage(Model model){
        model.addAttribute("endTime", DateUtil.getCurrentDayTime());
        model.addAttribute("startTime", DateUtil.getPreNDay(-7));
        return "alarmLog/list";
    }

    @PostMapping("/api/V1/saveData")
    @ResponseBody
    public Result saveData(@RequestBody String body, HttpServletRequest request){
        String type = request.getParameter("type");
        JSONObject jsonObject = JSONUtil.parseObj(body);
        String data = jsonObject.get("data").toString();
        String deviceId = jsonObject.get("deviceId").toString();
        String apiKey = jsonObject.get("apikey").toString();
        return displacementService.dealData(type, deviceId, data);
    }

    @PostMapping("/deviceData/query")
    @ResponseBody
    public Flexigrid queryDeviceData(Device device, Flexigrid flex, HttpServletRequest request){
        String rows = request.getParameter("rows");
        PageView<DeviceData> pageView = new PageView<>(flex.getPage());
        pageView.setPageSize(Integer.parseInt(rows));
        pageView = displacementService.queryDeviceData(pageView, device);
        flex.setTotal(pageView.getRowCount());
        flex.setRows(pageView.getRecords());
        return flex;
    }

    @PostMapping("/deviceData/queryAll")
    @ResponseBody
    public Result queryByTime(Device device, @RequestParam Integer deviceId){
        device.setId(deviceId);
        return displacementService.queryAll(device);
    }

    @PostMapping("/alarmLog/query")
    @ResponseBody
    public Flexigrid queryAlarmLog(AlarmLog alarmLog, Flexigrid flex, HttpServletRequest request){
        String rows = request.getParameter("rows");
        PageView<AlarmLog> pageView = new PageView<>(flex.getPage());
        pageView.setPageSize(Integer.parseInt(rows));
        pageView = alarmLogService.queryAlarmLog(pageView, alarmLog);
        flex.setTotal(pageView.getRowCount());
        flex.setRows(pageView.getRecords());
        return flex;
    }

    @PostMapping("/alarmPara/getByObj")
    @ResponseBody
    public Result getAlarmPara(AlarmPara alarmPara){
        return alarmParaService.getAlarmPara(alarmPara);
    }

    @PostMapping("/alarmPara/save")
    @ResponseBody
    public Result saveAlarmPara(AlarmPara alarmPara){
        return alarmParaService.saveAlarmPara(alarmPara);
    }

}
